<?php

/*
info: 图片处理类
date: 2011-01-11
*/

if (!defined('WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

if (defined('IMAGE_CLASS'))
{
	return;
}
define('IMAGE_CLASS', TRUE);

class Image
{
	public $errorMsg = '';
	private $im = '';
	private $ni = '';
	private $resizePlace = array(
		'imageWidth' => 0,
		'imageHeight' => 0,
		'dstX' => 0,
		'dstY' => 0,
		'srcX' => 0,
		'srcY' => 0,
		'dstWidth' => 0,
		'dstHeight' => 0,
		'srcWidth' => 0,
		'srcHeight' => 0,
		'isturecolor' => 1,
		'bgcolor' => '',
		'transcolor' => ''
		);
	private $srcFileInfo = array(
		'file' => '',
		'type' => '',
		'width' => 0,
		'height' => 0
		);
	private $watermarkMode = 1;
	private $watermarkPicInfo = array(
		'file' => '',
		'type' => '',
		'width' => 0,
		'height' => 0,
		'pct' => 100,
		'transcolor' => ''
		);
	private $watermarkPicPlace = array(
		'imageWidth' => 0,
		'imageHeight' => 0,
		'dstX' => 0,
		'dstY' => 0,
		'srcX' => 0,
		'srcY' => 0,
		'srcWidth' => 0,
		'srcHeight' => 0
		);
	private $watermarkTextInfo = array(
		'text' => '',
		'font' => '',
		'size' => 0,
		'angle' => 0,
		'mode' => 0,
		'space' => 1,
		'color' => '',
		'bgcolor' => ''
		);
	private $watermarkTextPlace = array(
		'textWidth' => 0,
		'textHeight' => 0,
		'fontX' => 0,
		'fontY' => 0
		);

//构造函数，初始化源文件
	public function Image($strSrcFile = '')
	{
		if (!empty($strSrcFile))
		{
			$this->SetSrcFile($strSrcFile);
		}
	}

//检测支持文件类型
	public function CheckType($strType)
	{
		$strType = strtoupper($strType);
		$setType = array('GIF' => IMG_GIF, 'JPG' => IMG_JPG, 'PNG' => IMG_PNG);
		if ($strType!='' && isset($setType[$strType]) && (imagetypes() & $setType[$strType]))
		{
			return TRUE;
		}
		else
		{
			$this->errorMsg = '不支持该文件类型';
			return FALSE;
		}
	}

//销毁资源
//$intType <0:源资源 0:全部资源 >0:目标资源
	public function Destroy($intType = 0)
	{
		if ($intType<=0 && is_resource($this->im))
		{
			imagedestroy($this->im);
		}
		if ($intType>=0 && is_resource($this->ni))
		{
			imagedestroy($this->ni);
		}
	}

//处理前初始化
//$intForce 是否强制恢复源资源 0:否 >0:是
	public function DoInit($intForce = 0)
	{
		if ($intForce>0 || !is_resource($this->ni))
		{
			$this->ni = $this->im;
		}
		if (!is_resource($this->ni))
		{
			$this->errorMsg = '没有设置源图片';
			return FALSE;
		}
		return TRUE;
	}

//制作缩略图
	public function Resize()
	{
		if (!$this->DoInit())
		{
			$this->errorMsg = '没有设置源图片';
			return FALSE;
		}
		elseif ($this->resizePlace['dstWidth']<=0 || $this->resizePlace['dstHeight']<=0)
		{
			$this->errorMsg = '没有设置缩略图尺寸';
			return FALSE;
		}

		if ($this->resizePlace['isturecolor']>0 && function_exists('imagecreatetruecolor'))
		{
			$tempni = imagecreatetruecolor($this->resizePlace['imageWidth'], $this->resizePlace['imageHeight']);
			if (empty($this->resizePlace['bgcolor']))
			{
				$bgcolor = $this->FormatColor($tempni, '#ffffff');
			}
			else
			{
				$bgcolor = $this->FormatColor($tempni, $this->resizePlace['bgcolor']);
			}
			imagefilledrectangle($tempni, 0, 0, $this->resizePlace['imageWidth'], $this->resizePlace['imageHeight'], $bgcolor);
			if (empty($this->resizePlace['bgcolor']))
			{
				imagecolortransparent($tempni, $bgcolor);
			}
			imagecopyresampled($tempni, $this->ni, $this->resizePlace['dstX'], $this->resizePlace['dstY'], $this->resizePlace['srcX'], $this->resizePlace['srcY'], $this->resizePlace['dstWidth'], $this->resizePlace['dstHeight'], $this->resizePlace['srcWidth'], $this->resizePlace['srcHeight']);
			if (!empty($this->resizePlace['transcolor']))
			{
				imagecolortransparent($tempni, $this->FormatColor($tempni, $this->resizePlace['transcolor']));
			}
		}
		elseif (function_exists('imagecreate'))
		{
			$tempni = imagecreate($this->resizePlace['imageWidth'], $this->resizePlace['imageHeight']);
			if (!empty($this->resizePlace['bgcolor']))
			{
				$bgcolor = $this->FormatColor($tempni, $this->resizePlace['bgcolor']);
				imagefilledrectangle($tempni, 0, 0, $this->resizePlace['imageWidth'], $this->resizePlace['imageHeight'], $bgcolor);
			}
			imagecopyresized($tempni, $this->ni, $this->resizePlace['dstX'], $this->resizePlace['dstY'], $this->resizePlace['srcX'], $this->resizePlace['srcY'], $this->resizePlace['dstWidth'], $this->resizePlace['dstHeight'], $this->resizePlace['srcWidth'], $this->resizePlace['srcHeight']);
		}
		else
		{
			$this->errorMsg = '系统不支持生成缩略图';
			return FALSE;
		}

		$this->ni = $tempni;
		return TRUE;
	}

//保存文件
	public function SaveFile($strDstFile = '', $intQuality = 80)
	{
		$strDstFile = empty($strDstFile) ? $this->srcFileInfo['file'] : $strDstFile;
		if (empty($strDstFile))
		{
			$this->errorMsg = '没有设置要保存的文件名';
			return FALSE;
		}
		$dstFileExt = strtoupper(global_FileExt($strDstFile));
		if (!$this->CheckType($dstFileExt))
		{
			$this->errorMsg = '不支持该文件类型';
			return FALSE;
		}
		elseif (!is_resource($this->ni))
		{
			$this->errorMsg = '没有可以保存的文件';
			return FALSE;
		}

		switch ($dstFileExt)
		{
		case 'GIF':
			imagegif($this->ni, $strDstFile);
			break;
		case 'JPG':
			imagejpeg($this->ni, $strDstFile, $intQuality);
			break;
		case 'PNG':
			if (function_exists('imagesavealpha'))
			{
				imagealphablending($this->ni, FALSE);
				imagesavealpha($this->ni, TRUE);
			}
			imagepng($this->ni, $strDstFile);
			break;
		}

		return TRUE;
	}

//显示文件
	public function ShowFile($strType = 'JPG', $intQuality = 80)
	{
		$strType = strtoupper($strType);
		if (!$this->CheckType($strType))
		{
			$this->errorMsg = '不支持该文件类型';
			return FALSE;
		}
		elseif (!is_resource($this->ni))
		{
			$this->errorMsg = '没有可以显示的图片';
			return FALSE;
		}

		switch ($strType)
		{
		case 'GIF':
			header("Content-type: image/gif");
			imagegif($this->ni);
			break;
		case 'JPG':
			header("Content-type: image/jpeg");
			imagejpeg($this->ni, '', $intQuality);
			break;
		case 'PNG':
			if (function_exists('imagesavealpha'))
			{
				imagealphablending($this->ni, FALSE);
				imagesavealpha($this->ni, TRUE);
			}
			header("Content-type: image/png");
			imagepng($this->ni);
			break;
		}

		return TRUE;
	}

//设置缩略图大小及裁切方案
//$intDstWidth 目标宽度  $intDstHeight 目标高度
//$intType 缩略类型 0:全部缩小 1:尺寸填满(左上) 2:尺寸填满(中上) 5:尺寸填满(中) 123:定宽(高自动) 147:定高(宽自动)
//$intScale 比例缩放 0:否(和规定尺寸一样) >0:是(尺寸不大于规定尺寸)
	public function SetResize($intDstWidth, $intDstHeight, $intType = 0, $intScale = 0)
	{
		$intSrcWidth = $this->srcFileInfo['width'];
		$intSrcHeight = $this->srcFileInfo['height'];
		if ($intSrcWidth<=0 || $intSrcHeight<=0 || $intDstWidth<=0 || $intDstHeight<=0)
		{
			$this->errorMsg = '参数设置错误';
			return FALSE;
		}

		$returnArray = array(
		'imageWidth' => $intDstWidth,
		'imageHeight' => $intDstHeight,
		'dstX' => 0,
		'dstY' => 0,
		'srcX' => 0,
		'srcY' => 0,
		'dstWidth' => $intDstWidth,
		'dstHeight' => $intDstHeight,
		'srcWidth' => $intSrcWidth,
		'srcHeight' => $intSrcHeight,
		'isturecolor' => 1,
		'bgcolor' => '#ffffff',
		'transcolor' => ''
		);

		$widthRate = $intSrcWidth/$intDstWidth;
		$heightRate = $intSrcHeight/$intDstHeight;

		if ($widthRate<=1 && $heightRate<=1)
		{
			$returnArray['dstWidth'] = $intSrcWidth;
			$returnArray['dstHeight'] = $intSrcHeight;
			switch ($intType)
			{
			//定宽
			case 123:
				$returnArray['imageHeight'] = $intSrcHeight;
				break;
			//定高
			case 147:
				$returnArray['imageWidth'] = $intSrcWidth;
				break;
			}
		}
		else
		{
			switch ($intType)
			{
			//全部
			case 0:
				if ($widthRate > $heightRate)
				{
					$returnArray['dstHeight'] = floor($intSrcHeight/$widthRate);
				}
				elseif ($widthRate < $heightRate)
				{
					$returnArray['dstWidth'] = floor($intSrcWidth/$heightRate);
				}
				break;
			//左上
			case 1:
				if ($widthRate<=1)
				{
					$returnArray['dstWidth'] = $intSrcWidth;
					$returnArray['srcHeight'] = $intDstHeight;
				}
				elseif ($heightRate<=1)
				{
					$returnArray['dstHeight'] = $intSrcHeight;
					$returnArray['srcWidth'] = $intDstWidth;
				}
				elseif ($widthRate > $heightRate)
				{
					$returnArray['srcWidth'] = round($intDstWidth*$heightRate);
				}
				elseif ($widthRate < $heightRate)
				{
					$returnArray['srcHeight'] = round($intDstHeight*$widthRate);
				}
				break;
			//中上
			case 2:
				if ($widthRate<=1)
				{
					$returnArray['dstWidth'] = $intSrcWidth;
					$returnArray['srcHeight'] = $intDstHeight;
				}
				elseif ($heightRate<=1)
				{
					$returnArray['dstHeight'] = $intSrcHeight;
					$returnArray['srcWidth'] = $intDstWidth;
					$returnArray['srcX'] = floor(($intSrcWidth-$returnArray['srcWidth'])/2);
				}
				elseif ($widthRate > $heightRate)
				{
					$returnArray['srcWidth'] = round($intDstWidth*$heightRate);
					$returnArray['srcX'] = floor(($intSrcWidth-$returnArray['srcWidth'])/2);
				}
				elseif ($widthRate < $heightRate)
				{
					$returnArray['srcHeight'] = round($intDstHeight*$widthRate);
				}
				break;
			//中
			case 5:
				if ($widthRate<=1)
				{
					$returnArray['dstWidth'] = $intSrcWidth;
					$returnArray['srcHeight'] = $intDstHeight;
					$returnArray['srcY'] = floor(($intSrcHeight-$returnArray['srcHeight'])/2);
				}
				elseif ($heightRate<=1)
				{
					$returnArray['dstHeight'] = $intSrcHeight;
					$returnArray['srcWidth'] = $intDstWidth;
					$returnArray['srcX'] = floor(($intSrcWidth-$returnArray['srcWidth'])/2);
				}
				elseif ($widthRate > $heightRate)
				{
					$returnArray['srcWidth'] = round($intDstWidth*$heightRate);
					$returnArray['srcX'] = floor(($intSrcWidth-$returnArray['srcWidth'])/2);
				}
				elseif ($widthRate < $heightRate)
				{
					$returnArray['srcHeight'] = round($intDstHeight*$widthRate);
					$returnArray['srcY'] = floor(($intSrcHeight-$returnArray['srcHeight'])/2);
				}
				break;
			//定宽
			case 123:
				if ($widthRate<=1)
				{
					$returnArray['dstWidth'] = $intSrcWidth;
					$returnArray['dstHeight'] = $returnArray['imageHeight'] = $intSrcHeight;
				}
				else
				{
					$returnArray['dstHeight'] = $returnArray['imageHeight'] = floor($intSrcHeight/$widthRate);
				}
				break;
			//定高
			case 147:
				if ($heightRate<=1)
				{
					$returnArray['dstHeight'] = $intSrcHeight;
					$returnArray['dstWidth'] = $returnArray['imageWidth'] = $intSrcWidth;
				}
				else
				{
					$returnArray['dstWidth'] = $returnArray['imageWidth'] = floor($intSrcWidth/$heightRate);
				}
				break;
			//默认
			default:
				$this->errorMsg = '缩略模式设置错误';
				return FALSE;
			}
		}

		if ($intScale > 0)
		{
			$returnArray['imageWidth'] = $returnArray['dstWidth'];
			$returnArray['imageHeight'] = $returnArray['dstHeight'];
		}
		else
		{
			$returnArray['dstX'] = floor(($returnArray['imageWidth']-$returnArray['dstWidth'])/2);
			$returnArray['dstY'] = floor(($returnArray['imageHeight']-$returnArray['dstHeight'])/2);
		}

		$this->SetResizePlace($returnArray);
		return TRUE;
	}

//设置缩略图参数
	public function SetResizePlace($aryPlace)
	{
		if (is_array($aryPlace) && !empty($aryPlace))
		{
			foreach ($this->resizePlace as $key => $value)
			{
				if (isset($aryPlace[$key]))
				{
					$this->resizePlace[$key] = $aryPlace[$key];
				}
			}
		}
	}

//设置源文件
	public function SetSrcFile($strSrcFile)
	{
		$tempinfo = $this->GetFileInfo($strSrcFile);
		if (empty($tempinfo))
		{
			$this->errorMsg = '错误的图片格式';
			return FALSE;
		}
		elseif (!$this->CheckType($tempinfo['type']))
		{
			$this->errorMsg = '不支持该文件类型';
			return FALSE;
		}

		$this->srcFileInfo['file'] = $tempinfo['file'];
		$this->srcFileInfo['type'] = $tempinfo['type'];
		$this->srcFileInfo['width'] = $tempinfo['width'];
		$this->srcFileInfo['height'] = $tempinfo['height'];

		$this->im = $this->CreateResource($this->srcFileInfo['file'], $this->srcFileInfo['type']);
		if (empty($this->im))
		{
			$this->errorMsg = '从源图片创建资源失败';
			return FALSE;
		}

		return TRUE;
	}

//设置水印参数
//$intMode 水印模式 0:文字 >0:图片
//$aryConfig 配置参数数组
//	文字: text 水印内容  font 文字字体  size 文字大小  mode 文字模式 0:默认 1:投影 2:描边 3:背景  color 前景色  bgcolor 背景色
//	图片: file 水印图片  pct 融合度0-100 0:透明 100:不透明  transcolor 透明颜色  resize 自适应大小 0:否 1:适应(最多覆盖长宽的1/3)
//$intPlace 水印位置 0:随机 1:左上 2:中上 3:右上 4:左中 5:中 6:右中 7:左下 8:中下 9:右下
//$intMargin 水印与图片边缘的空白间距
	public function SetWatermark($intMode, $aryConfig, $intPlace = 9, $intMargin = 5)
	{
		if (!$this->DoInit())
		{
			$this->errorMsg = '没有设置源图片';
			return FALSE;
		}
		$srcWidth = $resWidth = imagesx($this->ni);
		$srcHeight = $resHeight = imagesy($this->ni);
		$intMargin = abs(intval($intMargin));
		if ($intMargin > 0)
		{
			$resWidth = $srcWidth-$intMargin*2;
			$resHeight = $srcHeight-$intMargin*2;
		}
		if ($resWidth<=10 || $resHeight<=10)
		{
			$this->errorMsg = '图片太小或者间距设置太大不能完成水印操作';
			return FALSE;
		}

		//图片
		if ($intMode > 0)
		{
			$aryConfig['file'] = isset($aryConfig['file']) ? $aryConfig['file'] : '';
			$aryConfig['transcolor'] = isset($aryConfig['transcolor']) ? $aryConfig['transcolor'] : '';
			if (isset($aryConfig['pct']) && intval($aryConfig['pct'])>0 && intval($aryConfig['pct'])<=100)
			{
				$aryConfig['pct'] = intval($aryConfig['pct']);
			}
			else
			{
				$aryConfig['pct'] = 100;
			}
			$aryConfig['resize'] = (isset($aryConfig['resize']) && $aryConfig['resize']>0) ? 1 : 0;

			$picInfo = $this->GetFileInfo($aryConfig['file']);
			if (empty($picInfo))
			{
				$this->errorMsg = '错误的水印图片';
				return FALSE;
			}
			elseif (!$this->CheckType($picInfo['type']))
			{
				$this->errorMsg = '不支持水印文件类型';
				return FALSE;
			}
			elseif ($aryConfig['resize']==0 && ($picInfo['width']>$resWidth || $picInfo['height']>$resHeight))
			{
				$this->errorMsg = '水印图片超过了原图尺寸';
				return FALSE;
			}
			
			if ($aryConfig['resize']==0)
			{
				$waterWidth = $picInfo['width'];
				$waterHeight = $picInfo['height'];
			}
			else
			{
				$widthRate = $resWidth/$picInfo['width'];
				$heightRate = $resHeight/$picInfo['height'];
				if ($widthRate>=3 && $heightRate>=3)
				{
					$waterWidth = $picInfo['width'];
					$waterHeight = $picInfo['height'];
				}
				elseif ($widthRate < $heightRate)
				{
					$waterWidth = floor($resWidth/3);
					$waterHeight = floor($picInfo['height']/($picInfo['width']/$waterWidth));
				}
				elseif ($widthRate > $heightRate)
				{
					$waterHeight = floor($resHeight/3);
					$waterWidth = floor($picInfo['width']/($picInfo['height']/$waterHeight));
				}
				else
				{
					$waterWidth = floor($resWidth/3);
					$waterHeight = floor($resHeight/3);
				}
			}
			if ($waterWidth<=1 || $waterHeight<=1)
			{
				$this->errorMsg = '图片太小不能完成水印操作';
				return FALSE;
			}

			$this->watermarkMode = 1;

			$this->watermarkPicInfo['file'] = $aryConfig['file'];
			$this->watermarkPicInfo['type'] = $picInfo['type'];
			$this->watermarkPicInfo['width'] = $picInfo['width'];
			$this->watermarkPicInfo['height'] = $picInfo['height'];
			$this->watermarkPicInfo['pct'] = $aryConfig['pct'];
			$this->watermarkPicInfo['transcolor'] = $aryConfig['transcolor'];

			list($waterX, $waterY) = $this->GetWatermarkPlace($resWidth, $resHeight, $waterWidth, $waterHeight, $intPlace);
			if ($waterX>=0 && $waterY>=0)
			{
				$picPlace = array();
				$picPlace['imageWidth'] = $srcWidth;
				$picPlace['imageHeight'] = $srcHeight;
				$picPlace['dstX'] = $waterX+$intMargin;
				$picPlace['dstY'] = $waterY+$intMargin;
				$picPlace['srcX'] = 0;
				$picPlace['srcY'] = 0;
				$picPlace['srcWidth'] = $waterWidth;
				$picPlace['srcHeight'] = $waterHeight;
				$this->SetWatermarkPicPlace($picPlace);
			}

			return TRUE;
		}
		//文字
		else
		{
			if (empty($aryConfig['text']))
			{
				$this->errorMsg = '没有设置水印文字';
				return FALSE;
			}
			elseif (empty($aryConfig['font']) || !file_exists($aryConfig['font']))
			{
				$this->errorMsg = '水印字体文件设置错误';
				return FALSE;
			}
			$aryConfig['size'] = (isset($aryConfig['size']) && intval($aryConfig['size'])>0) ? intval($aryConfig['size']) : 9;
			$aryConfig['angle'] = (isset($aryConfig['angle'])) ? intval($aryConfig['angle']) : 0;
			$aryConfig['mode'] = (isset($aryConfig['mode'])) ? intval($aryConfig['mode']) : 0;
			$aryConfig['space'] = (isset($aryConfig['space']) && intval($aryConfig['space'])>0) ? intval($aryConfig['space']) : 1;
			$aryConfig['color'] = (isset($aryConfig['color'])) ? $aryConfig['color'] : '#000000';
			$aryConfig['bgcolor'] = (isset($aryConfig['bgcolor'])) ? $aryConfig['bgcolor'] : '';

			if (!function_exists('imagettfbbox'))
			{
				$this->errorMsg = '不支持文字水印生成';
				return FALSE;
			}
			$textBox = imagettfbbox($aryConfig['size'], $aryConfig['angle'], $aryConfig['font'], $aryConfig['text']);
			$waterWidth = max($textBox[2], $textBox[4])-min($textBox[0], $textBox[6]);
			$waterHeight = max($textBox[1], $textBox[3])-min($textBox[5], $textBox[7]);

			if ($waterWidth>$resWidth || $waterHeight>$resHeight)
			{
				$this->errorMsg = '水印区域超过图片区域不能完成水印操作';
				return FALSE;
			}

			$this->watermarkMode = 0;

			$this->watermarkTextInfo['text'] = $aryConfig['text'];
			$this->watermarkTextInfo['font'] = $aryConfig['font'];
			$this->watermarkTextInfo['size'] = $aryConfig['size'];
			$this->watermarkTextInfo['angle'] = $aryConfig['angle'];
			$this->watermarkTextInfo['mode'] = $aryConfig['mode'];
			$this->watermarkTextInfo['space'] = $aryConfig['space'];
			$this->watermarkTextInfo['color'] = $aryConfig['color'];
			$this->watermarkTextInfo['bgcolor'] = $aryConfig['bgcolor'];

			list($waterX, $waterY) = $this->GetWatermarkPlace($resWidth, $resHeight, $waterWidth, $waterHeight, $intPlace);
			if ($waterX>=0 && $waterY>=0)
			{
				$textPlace = array();
				$textPlace['textWidth'] = $waterWidth;
				$textPlace['textHeight'] = $waterHeight;
				$textPlace['fontX'] = $waterX+$intMargin;
				$textPlace['fontY'] = $waterY+$intMargin+$waterHeight;
				$this->SetWatermarkTextPlace($textPlace);
			}

			return TRUE;
		}
	}

//设置图片水印参数
	public function SetWatermarkPicPlace($aryPlace)
	{
		if (is_array($aryPlace) && !empty($aryPlace))
		{
			foreach ($this->watermarkPicPlace as $key => $value)
			{
				if (isset($aryPlace[$key]))
				{
					$this->watermarkPicPlace[$key] = $aryPlace[$key];
				}
			}
		}
	}

//设置文字水印参数
	public function SetWatermarkTextPlace($aryPlace)
	{
		if (is_array($aryPlace) && !empty($aryPlace))
		{
			foreach ($this->watermarkTextPlace as $key => $value)
			{
				if (isset($aryPlace[$key]))
				{
					$this->watermarkTextPlace[$key] = $aryPlace[$key];
				}
			}
		}
	}

//制作水印图
	public function Watermark()
	{
		if (!is_resource($this->ni))
		{
			$this->errorMsg = '没有设置源图片';
			return FALSE;
		}
		//图片
		if ($this->watermarkMode > 0)
		{
			if (empty($this->watermarkPicInfo['file']) || $this->watermarkPicPlace['srcWidth']<=0 || $this->watermarkPicPlace['srcWidth']<=0)
			{
				$this->errorMsg = '没有设置水印图片及位置';
				return FALSE;
			}

			$tempni = $this->ni;
			$this->ni = $this->CreateResource($this->watermarkPicInfo['file'], $this->watermarkPicInfo['type']);
			if (empty($this->ni))
			{
				$this->errorMsg = '从水印图片创建资源失败';
				return FALSE;
			}

			if (!empty($this->watermarkPicInfo['transcolor']) || $this->watermarkPicPlace['srcWidth']<$this->watermarkPicInfo['width'] || $this->watermarkPicPlace['srcHeight']<$this->watermarkPicInfo['height'])
			{
				$tempResize = array(
					'imageWidth' => $this->watermarkPicPlace['srcWidth'],
					'imageHeight' => $this->watermarkPicPlace['srcHeight'],
					'dstX' => 0,
					'dstY' => 0,
					'srcX' => 0,
					'srcY' => 0,
					'dstWidth' => $this->watermarkPicPlace['srcWidth'],
					'dstHeight' => $this->watermarkPicPlace['srcHeight'],
					'srcWidth' => $this->watermarkPicInfo['width'],
					'srcHeight' => $this->watermarkPicInfo['height'],
					'isturecolor' => 1,
					'bgcolor' => ''
					);
				$tempResize['transcolor'] = (!empty($this->watermarkPicInfo['transcolor']) && $this->watermarkPicInfo['transcolor']{0}=='#') ? $this->watermarkPicInfo['transcolor'] : '';
				$this->SetResizePlace($tempResize);
				if (!$this->Resize())
				{
					$this->errorMsg = '按比例生成水印失败';
					return FALSE;
				}
			}
			
			imagealphablending($tempni, TRUE);
			if (function_exists('imagecopymerge'))
			{
				imagecopymerge($tempni, $this->ni, $this->watermarkPicPlace['dstX'], $this->watermarkPicPlace['dstY'], $this->watermarkPicPlace['srcX'], $this->watermarkPicPlace['srcY'], $this->watermarkPicPlace['srcWidth'], $this->watermarkPicPlace['srcHeight'], $this->watermarkPicInfo['pct']);
			}
			elseif (function_exists('imagecopy'))
			{
				imagecopy($tempni, $this->ni, $this->watermarkPicPlace['dstX'], $this->watermarkPicPlace['dstY'], $this->watermarkPicPlace['srcX'], $this->watermarkPicPlace['srcY'], $this->watermarkPicPlace['srcWidth'], $this->watermarkPicPlace['srcHeight']);
			}
			else
			{
				$this->errorMsg = '系统不支持图片水印生成';
				return FALSE;
			}

			$this->ni = $tempni;
			return TRUE;
		}
		//文字
		else
		{
			if (empty($this->watermarkTextInfo['text']) || $this->watermarkTextPlace['textWidth']<=0 || $this->watermarkTextPlace['textHeight']<=0)
			{
				$this->errorMsg = '没有设置水印文字及位置';
				return FALSE;
			}
			elseif (!function_exists('imagettftext'))
			{
				$this->errorMsg = '系统不支持文字水印生成';
				return FALSE;
			}

			if (!empty($this->watermarkTextInfo['bgcolor']))
			{
				$bgcolor = $this->FormatColor($this->ni, $this->watermarkTextInfo['bgcolor']);
				switch ($this->watermarkTextInfo['mode'])
				{
				//投影
				case 1:
					imagettftext($this->ni, $this->watermarkTextInfo['size'], $this->watermarkTextInfo['angle'], $this->watermarkTextPlace['fontX']+$this->watermarkTextInfo['space'], $this->watermarkTextPlace['fontY']+$this->watermarkTextInfo['space'], $bgcolor, $this->watermarkTextInfo['font'], $this->watermarkTextInfo['text']);
					break;
				//描边
				case 2:
					imagettftext($this->ni, $this->watermarkTextInfo['size'], $this->watermarkTextInfo['angle'], $this->watermarkTextPlace['fontX']-$this->watermarkTextInfo['space'], $this->watermarkTextPlace['fontY']-$this->watermarkTextInfo['space'], $bgcolor, $this->watermarkTextInfo['font'], $this->watermarkTextInfo['text']);
					imagettftext($this->ni, $this->watermarkTextInfo['size'], $this->watermarkTextInfo['angle'], $this->watermarkTextPlace['fontX']+$this->watermarkTextInfo['space'], $this->watermarkTextPlace['fontY']-$this->watermarkTextInfo['space'], $bgcolor, $this->watermarkTextInfo['font'], $this->watermarkTextInfo['text']);
					imagettftext($this->ni, $this->watermarkTextInfo['size'], $this->watermarkTextInfo['angle'], $this->watermarkTextPlace['fontX']-$this->watermarkTextInfo['space'], $this->watermarkTextPlace['fontY']+$this->watermarkTextInfo['space'], $bgcolor, $this->watermarkTextInfo['font'], $this->watermarkTextInfo['text']);
					imagettftext($this->ni, $this->watermarkTextInfo['size'], $this->watermarkTextInfo['angle'], $this->watermarkTextPlace['fontX']+$this->watermarkTextInfo['space'], $this->watermarkTextPlace['fontY']+$this->watermarkTextInfo['space'], $bgcolor, $this->watermarkTextInfo['font'], $this->watermarkTextInfo['text']);
					break;
				//背景
				case 3:
					imagefilledrectangle($this->ni, $this->watermarkTextPlace['fontX']-$this->watermarkTextInfo['space'], $this->watermarkTextPlace['fontY']-$this->watermarkTextPlace['textHeight']-$this->watermarkTextInfo['space'], $this->watermarkTextPlace['fontX']+$this->watermarkTextPlace['textWidth']+$this->watermarkTextInfo['space'], $this->watermarkTextPlace['fontY']+$this->watermarkTextInfo['space'], $bgcolor);
					break;
				}
			}
			$fontcolor = $this->FormatColor($this->ni, $this->watermarkTextInfo['color']);
			imagettftext($this->ni, $this->watermarkTextInfo['size'], $this->watermarkTextInfo['angle'], $this->watermarkTextPlace['fontX'], $this->watermarkTextPlace['fontY'], $fontcolor, $this->watermarkTextInfo['font'], $this->watermarkTextInfo['text']);

			return TRUE;
		}
	}


//----------------------------------------------------------------------------


/*
作用：	一次性缩略图生成保存
参数：	$strSrcFile 源文件  $intWidth 目标宽度  $intHeight 目标高度  $strDstFile 目标文件(为空为覆盖源文件)
		$intType 缩略类型 0:全部缩小 1:尺寸填满(左上) 2:尺寸填满(中上) 5:尺寸填满(中) 123:定宽(高自动) 147:定高(宽自动)
		$intScale 比例缩放 0:否(和规定尺寸一样) >0:是(尺寸不大于规定尺寸)
返回：	TRUE 或者 FALSE
*/
	public function OnceResize($strSrcFile, $intWidth, $intHeight, $strDstFile = '', $intType = 0, $intScale = 0)
	{
		if ($this->SetSrcFile($strSrcFile))
		{
			if ($this->SetResize($intWidth, $intHeight, $intType, $intScale))
			{
				if ($this->Resize())
				{
					if ($this->SaveFile($strDstFile))
					{
						$this->Destroy();
						return TRUE;
					}
				}
			}
		}
		$this->Destroy();
		return FALSE;
	}

/*
作用：	一次性水印图生成保存
参数：	$strSrcFile 源文件
		$intMode 水印模式 0:文字 >0:图片
		$aryConfig 配置参数数组
			文字: text 水印内容  font 文字字体  size 文字大小  mode 文字模式 0:默认 1:投影 2:描边 3:背景
				color 前景色  bgcolor 背景色
			图片: file 水印图片  pct 融合度0-100 0:透明 100:不透明  transcolor 透明颜色
				resize 自适应大小 0:否 1:适应(最多覆盖长宽的1/3)
		$strDstFile 目标文件(为空为覆盖源文件)
		$intPlace 水印位置 0:随机 1:左上 2:中上 3:右上 4:左中 5:中 6:右中 7:左下 8:中下 9:右下
		$intMargin 水印与图片边缘的空白间距
返回：	TRUE 或者 FALSE
*/
	public function OnceWatermark($strSrcFile, $intMode, $aryConfig, $strDstFile = '', $intPlace = 9, $intMargin = 5)
	{
		if ($this->SetSrcFile($strSrcFile))
		{
			if ($this->SetWatermark($intMode, $aryConfig, $intPlace, $intMargin))
			{
				if ($this->Watermark())
				{
					if ($this->SaveFile($strDstFile))
					{
						$this->Destroy();
						return TRUE;
					}
				}
			}
		}
		$this->Destroy();
		return FALSE;
	}


//----------------------------------------------------------------------------


//从图片创建资源
	private function CreateResource($strFile, $strType)
	{
		$tempim = '';
		$strType = strtoupper($strType);
		switch ($strType)
		{
		case 'GIF':
			$tempim = imagecreatefromgif($strFile);
			break;
		case 'JPG':
			$tempim = imagecreatefromjpeg($strFile);
			break;
		case 'PNG':
			$tempim = imagecreatefrompng($strFile);
			break;
		}
		return $tempim;
	}

//将web颜色转换为图像颜色
	private function FormatColor($resImage, $strColor)
	{
		if ($strColor{0}=='#')
		{
			$strColor = substr($strColor, 1);
		}
		$colordec = hexdec($strColor);
		return imagecolorallocate($resImage, ($colordec & 0xFF0000) >> 16, ($colordec & 0x00FF00) >> 8, ($colordec & 0x0000FF));
	}

//获取图片信息
	private function GetFileInfo($strFile)
	{
		$fileType = array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG');
		$returnArray = array();
		$data = getimagesize($strFile);
		if (empty($strFile) || $data===FALSE || $data[0]<=0 || $data[1]<=0 || !isset($fileType[$data[2]]))
		{
			return array();
		}
		else
		{
			$returnArray['file'] = $strFile;
			$returnArray['type'] = $fileType[$data[2]];
			$returnArray['width'] = $data[0];
			$returnArray['height'] = $data[1];
			return $returnArray;
		}
	}

//获取水印坐标
	private function GetWatermarkPlace($intResWidth, $intResHeight, $intWaterWidth, $intWaterHeight, $intPlace)
	{
		switch ($intPlace)
		{
		//随机
		case 0:
			$waterX = rand(0, ($intResWidth-$intWaterWidth));
			$waterY = rand(0, ($intResHeight-$intWaterHeight));
			break;
		//左上
		case 1:
			$waterX = 0;
			$waterY = 0;
			break;
		//中上
		case 2:
			$waterX = floor(($intResWidth-$intWaterWidth)/2);
			$waterY = 0;
			break;
		//右上
		case 3:
			$waterX = $intResWidth-$intWaterWidth;
			$waterY = 0;
			break;
		//左中
		case 4:
			$waterX = 0;
			$waterY = floor(($intResHeight-$intWaterHeight)/2);
			break;
		//中
		case 5:
			$waterX = floor(($intResWidth-$intWaterWidth)/2);
			$waterY = floor(($intResHeight-$intWaterHeight)/2);
			break;
		//右中
		case 6:
			$waterX = $intResWidth-$intWaterWidth;
			$waterY = floor(($intResHeight-$intWaterHeight)/2);
			break;
		//左下
		case 7:
			$waterX = 0;
			$waterY = $intResHeight-$intWaterHeight;
			break;
		//中下
		case 8:
			$waterX = floor(($intResWidth-$intWaterWidth)/2);
			$waterY = $intResHeight-$intWaterHeight;
			break;
		//右下
		case 9:
			$waterX = $intResWidth-$intWaterWidth;
			$waterY = $intResHeight-$intWaterHeight;
			break;
		//默认
		default:
			$waterX = -1;
			$waterY = -1;
		}
		return array($waterX, $waterY);
	}

}	//end class

?>